package com.neusoft.care.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.neusoft.care.dao.CareItemDao;
import com.neusoft.care.dao.LevelItemDao;
import com.neusoft.care.po.CareItem;
import com.neusoft.care.po.LevelItem;
import com.neusoft.care.po.ResponseBean;
import com.neusoft.care.service.CareItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CareItemServiceImpl implements CareItemService {

    @Autowired
    private CareItemDao careItemDao;
    @Autowired
    private LevelItemDao levelItemDao;


    @Override
//    多条件查询
    public ResponseBean<List<CareItem>> getCareItemsByCondition(String itemName, String status) {
        List<CareItem> careItems = careItemDao.getCareItemsByConditions(itemName, status);
        if (careItems != null && !careItems.isEmpty()) {
            return new ResponseBean<>(careItems);
        } else {
            return new ResponseBean<>(500, "护理项目查询失败");
        }
    }

    @Override
//    查询护理项目名称，默认启用
    public ResponseBean<List<CareItem>> getAllCareItems() {
        List<CareItem> careItems = careItemDao.selectList(new QueryWrapper<CareItem>().eq("status", "启用"));
        return new ResponseBean<>(careItems);
    }

    @Override
    public ResponseBean<CareItem> getCareItemById(Integer itemId) {
        CareItem careItem = careItemDao.selectById(itemId);
        if (careItem != null) {
            return new ResponseBean<>(careItem);
        } else {
            return new ResponseBean<>(404, "护理项目未找到");
        }
    }

    @Override
    public ResponseBean<Integer> addCareItem(CareItem careItem) {
        int result = careItemDao.insert(careItem);
        if (result > 0) {
            return new ResponseBean<>(result); // 返回插入成功的行数
        } else {
            return new ResponseBean<>(500, "护理项目添加失败");
        }
    }

    @Override
//    修改护理级别，状态修改为停用需要剔除护理级别中的项目
    public ResponseBean<Integer> updateCareItem(CareItem careItem) {
        String status = careItem.getStatus();
        if (status.equals("停用")) {
            List<LevelItem> levelItemList = levelItemDao.selectList(new QueryWrapper<LevelItem>().eq("item_id", careItem.getItemId()));
            if (levelItemList != null && !levelItemList.isEmpty()) {
                levelItemDao.deleteBatchIds(levelItemList);
            }
        }
        int result = careItemDao.updateById(careItem);
        if (result > 0) {
            return new ResponseBean<>(result); // 返回更新成功的行数
        } else {
            return new ResponseBean<>(500, "护理项目信息更新失败");
        }
    }

    @Override
    public ResponseBean<Integer> deleteCareItem(Integer itemId) {
        CareItem careItem = careItemDao.selectById(itemId);
        List<LevelItem> levelItemList = levelItemDao.selectList(new QueryWrapper<LevelItem>().eq("item_id", careItem.getItemId()));
        if (levelItemList != null && !levelItemList.isEmpty()) {
            levelItemDao.deleteBatchIds(levelItemList);
        }
        careItem.setIsActive("移除");
        careItem.setStatus("停用");
        int result = careItemDao.updateById(careItem);
        if (result > 0) {
            return new ResponseBean<>(result); // 返回更新成功的行数
        } else {
            return new ResponseBean<>(500, "护理项目信息删除失败");
        }
    }
}